﻿#include "MyToolButton.h"

MyToolButton::MyToolButton(QWidget *parent) : QToolButton(parent)
{
    //启用拖动事件
    this->setAcceptDrops(true);
}

//设置角色
void MyToolButton::setRole(roles role)
{
    mRole = role;
}

void MyToolButton::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    //如果是回收站单击就无效
    if(mRole == garbage)
    {
        return;
    }

    if(event->button() == Qt::LeftButton)
    {
        QDrag* drag = new QDrag(this);
        QMimeData* mindata = new QMimeData();
        QString text;
        switch (mRole) {
            case mainRole:text = "mainRole";break;
            case box:text = "box";break;
            case boxReq:text = "boxReq";break;
            case wall:text = "wall";break;
            case garbage:text = "garbage";break;;
        }
        mindata->setText(text);
        drag->setMimeData(mindata);
        drag->exec();
    }
}

void MyToolButton::dragEnterEvent(QDragEnterEvent *event)
{
    if(mRole != garbage)
    {
        event->ignore();
        return;
    }
    QString text = event->mimeData()->text();
    if(text == "move")
    {
        event->accept();
    }
    else {
        event->ignore();
    }
}

void MyToolButton::dropEvent(QDropEvent *event)
{
    QString text = event->mimeData()->text();
    if(text == "move")
    {
        emit sendDelete(moveRole->getPoint());
        event->accept();
    }
    else {
        event->ignore();
    }
}
